﻿using System;
using System.Xml.Serialization;

namespace Opo.Perspective.Core
{
    /// <summary>
    /// Represents a contact
    /// </summary>
    [XmlRoot("contact")]
    public class Contact : IPerspectiveEntity
    {
        private string _title;
        private string _summary;

        /// <summary>
        /// Gets or sets the display text of the contact
        /// </summary>
        /// <value>The title.</value>
        [XmlElement("title", IsNullable = true)]
        public string Title
        {
            get
            {
                if (String.IsNullOrEmpty(_title))
                {
                    return FirstName + " " + LastName;
                }
                else
                {
                    return _title;
                }
            }
            set
            {
                _title = value;
            }
        }
        /// <summary>
        /// Gets the summary of the contact
        /// </summary>
        /// <value>The summary of the contact.</value>
        [XmlElement("summary", IsNullable=true)]
        public string Summary
        {
            get
            {
                if (String.IsNullOrEmpty(_summary))
                {
                    return FirstName + " " + LastName;
                }
                else
                {
                    return _summary;
                }
            }
            protected set
            {
                _summary = value;
            }
        }
        /// <summary>
        /// Gets or sets the date.
        /// </summary>
        /// <value>The date.</value>
        [XmlElement("date")]
        public DateTime Date { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether this entity has been read.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this entity has been read; otherwise, <c>false</c>.
        /// </value>
        [XmlElement("isRead")]
        public bool IsRead { get; set; }
        /// <summary>
        /// Gets or sets a flag for the contact.
        /// </summary>
        /// <value>A flag.</value>
        [XmlElement("flag")]
        public IFlag Flag { get; set; }
        /// <summary>
        /// Gets or sets a tags collection.
        /// </summary>
        /// <value>A collection of tags</value>
        [XmlArray(ElementName = "tags")]
        [XmlArrayItem(ElementName = "tag", IsNullable = true)]
        public TagCollection Tags { get; set; }

        /// <summary>
        /// Gets or sets the contact ID.
        /// </summary>
        /// <value>The contact ID.</value>
        [XmlElement("contactID")]
        public Guid ID { get; set; }
        /// <summary>
        /// Gets or sets the first name.
        /// </summary>
        /// <value>The first name.</value>
        [XmlElement("firstName")]
        public string FirstName { get; set; }
        /// <summary>
        /// Gets or sets the last name.
        /// </summary>
        /// <value>The last name.</value>
        [XmlElement("lastName")]
        public string LastName { get; set; }
        /// <summary>
        /// Gets or sets a collection of contact info.
        /// </summary>
        /// <value>The collection of contact info.</value>
        [XmlArray(ElementName = "contactInfoCollection")]
        [XmlArrayItem(ElementName = "contactInfo", IsNullable = true)]
        public ContactInfoCollection ContactInfo { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="PerspectiveContact"/> class.
        /// </summary>
        public Contact() { }
        /// <summary>
        /// Initializes a new instance of the <see cref="PerspectiveContact"/> class.
        /// </summary>
        /// <param name="firstName">The first name.</param>
        /// <param name="lastName">The last name.</param>
        public Contact(string firstName, string lastName) : this(firstName, lastName, new ContactInfoCollection()) { }
        /// <summary>
        /// Initializes a new instance of the <see cref="PerspectiveContact"/> class.
        /// </summary>
        /// <param name="firstName">The first name.</param>
        /// <param name="lastName">The last name.</param>
        /// <param name="contactInfo">The contact info.</param>
        public Contact(string firstName, string lastName, ContactInfoCollection contactInfo)
        {
            ID = Guid.NewGuid();
            FirstName = firstName;
            LastName = lastName;
            ContactInfo = contactInfo;
        }
    }
}
